package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallCollect;
import com.cskaoyan.bean.CskaoyanmallFootprint;
import com.cskaoyan.bean.CskaoyanmallFootprintExample;
import com.cskaoyan.mapper.CskaoyanmallCollectMapper;
import com.cskaoyan.mapper.CskaoyanmallFootprintMapper;
import com.cskaoyan.service.FootprintService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FootprintServiceImpl implements FootprintService {
    @Autowired
    CskaoyanmallFootprintMapper cskaoyanmallFootprintMapper;

    @Override
    public Map<String, Object> querySelective(Integer userId, Integer goodsId, Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        CskaoyanmallFootprintExample footprintExample = new CskaoyanmallFootprintExample();
        CskaoyanmallFootprintExample.Criteria criteria = footprintExample.createCriteria();

        if (!ObjectUtils.isEmpty(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        if (!ObjectUtils.isEmpty(goodsId)){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        if (!ObjectUtils.isEmpty(sort)&&!ObjectUtils.isEmpty(order)){
            footprintExample.setOrderByClause(sort+" "+order);
        }
        List<CskaoyanmallFootprint> footprints = cskaoyanmallFootprintMapper.selectByExample(footprintExample);
        PageInfo<CskaoyanmallFootprint> footprintPageInfo = new PageInfo<>(footprints);
        long total = footprintPageInfo.getTotal();
        HashMap<String, Object> res = new HashMap<>();
        res.put("items",footprints);
        res.put("total",total);
        return res;
    }
}
